<template>
  <div class="modern-moments">
    <el-empty v-if="moments.length === 0" description="还没有记录任何心情" />

    <div v-else class="moment-cards">
      <div
        v-for="moment in moments"
        :key="moment.id"
        class="moment-card"
      >
        <div class="moment-header">
          <div class="moment-meta">
            <span class="moment-date">{{ formatDate(moment.createdAt) }}</span>
            <el-tag
              v-if="moment.mood"
              :type="getMoodTagType(moment.mood)"
              size="small"
            >
              {{ getMoodLabel(moment.mood) }}
            </el-tag>
            <el-tag v-if="moment.isPrivate" type="info" size="small">
              <el-icon><Lock /></el-icon>
              私密
            </el-tag>
          </div>
          <div class="moment-actions">
            <el-button
              size="small"
              text
              @click="$emit('edit', moment.id)"
            >
              <el-icon><Edit /></el-icon>
            </el-button>
            <el-button
              size="small"
              text
              type="danger"
              @click="$emit('delete', moment.id)"
            >
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
        </div>

        <div class="moment-content">
          <p class="moment-text">{{ moment.content }}</p>

          <div v-if="moment.images && moment.images.length > 0" class="moment-images">
            <el-image
              v-for="(image, index) in moment.images"
              :key="index"
              :src="image"
              fit="cover"
              class="moment-image"
              :preview-src-list="moment.images"
              :initial-index="index"
            />
          </div>

          <div v-if="moment.tags && moment.tags.length > 0" class="moment-tags">
            <el-tag
              v-for="tag in moment.tags"
              :key="tag"
              size="small"
              effect="plain"
              @click="$emit('search-tag', tag)"
              class="tag-clickable"
            >
              #{{ tag }}
            </el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Edit, Delete, Lock } from '@element-plus/icons-vue'
import type { Moment, MoodType } from '../types'

interface Props {
  moments: Moment[]
}

defineProps<Props>()

defineEmits<{
  edit: [id: string]
  delete: [id: string]
  'search-tag': [tag: string]
}>()

// 心情选项
const moodOptions = [
  { value: 'happy', label: '😊 开心' },
  { value: 'sad', label: '😢 难过' },
  { value: 'excited', label: '🎉 兴奋' },
  { value: 'peaceful', label: '😌 平静' },
  { value: 'anxious', label: '😰 焦虑' },
  { value: 'grateful', label: '🙏 感恩' },
  { value: 'thoughtful', label: '🤔 沉思' },
  { value: 'nostalgic', label: '😌 怀念' }
]

// 获取心情标签类型
const getMoodTagType = (mood: MoodType) => {
  const typeMap = {
    happy: 'success',
    excited: 'success',
    grateful: 'success',
    peaceful: 'info',
    thoughtful: 'info',
    nostalgic: 'info',
    sad: 'warning',
    anxious: 'danger'
  }
  return typeMap[mood as keyof typeof typeMap] || 'info'
}

// 获取心情标签文本
const getMoodLabel = (mood: MoodType) => {
  const option = moodOptions.find(opt => opt.value === mood)
  return option?.label || mood
}

// 格式化日期
const formatDate = (dateStr: string) => {
  const date = new Date(dateStr)
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))

  if (days === 0) {
    return '今天 ' + date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    })
  } else if (days === 1) {
    return '昨天 ' + date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    })
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return date.toLocaleDateString('zh-CN')
  }
}
</script>

<style scoped>
.modern-moments {
  height: 100%;
  overflow-y: auto;
  padding: 0 10px;
}

.moment-cards {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 20px;
}

.moment-card {
  background: var(--el-bg-color);
  border-radius: 12px;
  padding: 20px;
  border: 1px solid var(--el-border-color-light);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  transition: all 0.2s ease;
}

.moment-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.moment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.moment-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.moment-date {
  font-size: 13px;
  color: var(--el-text-color-regular);
}

.moment-actions {
  display: flex;
  gap: 4px;
}

.moment-content {
  line-height: 1.6;
}

.moment-text {
  margin: 0 0 12px 0;
  font-size: 15px;
  color: var(--el-text-color-primary);
  white-space: pre-wrap;
}

.moment-images {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.moment-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  cursor: pointer;
}

.moment-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tag-clickable {
  cursor: pointer;
  transition: all 0.2s ease;
}

.tag-clickable:hover {
  transform: scale(1.05);
}
</style>
